/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.akro.utl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 *
 * @author Jose Carlos Palma
 * @version v1.0
 */
public class Configuration {

    private File file;
    private HashMap<String, Property> properties;

    public Configuration(String fileName) throws FileNotFoundException {
        this(new File(fileName));
    }

    public Configuration(File file) throws FileNotFoundException {
        this.file = file;
        this.properties = new HashMap<String, Property>();
        load();
    }

    private void load() throws FileNotFoundException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String key = null;
            String value = null;
            String line = null;

            while ((line = br.readLine()) != null) {
                String prop[] = line.trim().split("[=]+", 2);
                key = prop[0].trim();
                if (prop.length == 2) {
                    value = prop[1].trim();
                } else {
                    value = "";
                }

                if (key.length() > 0 && !key.matches("^[#;](.)*")) {
                    //System.out.printf("[%1$s: %2$s]\n", key, value);
                    this.properties.put(key, new Property(value));
                }
            }
        } catch (FileNotFoundException ex) {
            throw ex;
        } catch (IOException ex) {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex2) {
                }
            }
        }


    }

    public Property getValue(String key){
        return this.properties.get(key);
    }

    public String getStringValue(String key) {
        Property pro = this.properties.get(key);
        if (pro == null) {
            return null;
        }
        return pro.getStringValue();
    }

    public int getIntValue(String key){
        Property pro = this.properties.get(key);
        if (pro == null) {
            return -1;
        }
        return pro.getIntValue();
    }

    public long getLongValue(String key){
        Property pro = this.properties.get(key);
        if (pro == null) {
            return -1;
        }
        return pro.getLongValue();
    }

    public double getDoubleValue(String key){
        Property pro = this.properties.get(key);
        if (pro == null) {
            return -1;
        }
        return pro.getDoubleValue();
    }

    public String[] getListValue(String key){
        Property pro = this.properties.get(key);
        if (pro == null) {
            return null;
        }
        return pro.getListValue();
    }
    
}
